<?php

namespace Admin\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Select;


class MenuTable extends AbstractTableGateway {

    protected $table = 'chuyenmuc';

    public function __construct(Adapter $adapter) {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Menu());

        $this->initialize();
    }

    public function fetchAll(Select $select = null) {
        if (null === $select)
            $select = new Select();
        $select->from($this->table,array('*'))
				->join(
					'theloai',
					'theloai.id_theloai = chuyenmuc.theloai',
					array('*'));
        $resultSet = $this->selectWith($select);
        $resultSet->buffer();
        return $resultSet;
    }

    public function getMenu($id) {
        $id = (int) $id;
        $rowset = $this->select(array('id' => $id));
        $row = $rowset->current(); 
        return $row;
    }

    public function saveMenu(Menu $menu) {
		
			
		
        

        $id = (int) $menu->id;
        if ($id == 0) {
			$data = array(
				'tenchuyenmuc' => $menu->tenchuyenmuc,
				'duongdan' => $this->stripUnicode($menu->tenchuyenmuc),
				'theloai' => $menu->theloai
			);
            	
                $this->insert($data);
            
        } else {
			
			$data = array(
				'tenchuyenmuc' => $menu->tenchuyenmuc,
				'duongdan' => $this->stripUnicode($menu->tenchuyenmuc),
				'theloai' => $menu->theloai
				
			);
            if ($this->getMenu($id)) {
				
					$this->update($data, array('id' => $id));
				
            } else {
                throw new \Exception('Không tồn tại');
            }
        }
    }

    public function deleteMenu($id) {
        $this->delete(array('id' => $id));
    }
	public function xoaMenu($id) {
		
        $this->delete('id IN (' . $id . ')');
    }
	
	//kiem tra ton tai
	public function check_exists($tenchuyenmuc,$id)
	{
		//$rowset = $this->select(array('title' => $title));
        //$row = $rowset->current();
		$resultSet = $this->select(array('tenchuyenmuc = "'.$tenchuyenmuc.'"', 'id != "'.$id.'"'));
    	$row = $resultSet->current();
        return $row;
		
	}
    function stripUnicode($str){
	  if(!$str) return false;
	   $unicode = array(
		  'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ|Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ|A',
		  'd'=>'đ|Đ|D',
		  'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ|É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ|E',
		  'i'=>'í|ì|ỉ|ĩ|ị|Í|Ì|Ỉ|Ĩ|Ị|I',
		  'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ|Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ|O',
		  'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự|Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự|U',
		  'y'=>'ý|ỳ|ỷ|ỹ|ỵ|Ý|Ỳ|Ỷ|Ỹ|Ỵ|Y',
		  'b'=>'B',
		  'c'=>'C',
		  'f'=>'F',
		  'g'=>'G',
		  'h'=>'H',
		  'j'=>'J',
		  'k'=>'K',
		  'm'=>'M',
		  'n'=>'N',
		  'p'=>'P',
		  'q'=>'Q',
		  'r'=>'R',
		  's'=>'s',
		  't'=>'T',
		  'v'=>'V',
		  'w'=>'W',
		  'x'=>'X',
		  'z'=>'Z',
 
	   );
	foreach($unicode as $nonUnicode=>$uni) $str = preg_replace("/($uni)/i",$nonUnicode,$str);
	$str = str_replace( ' ', '-', $str );
	$str = str_replace( ';', '', $str );
	$str = str_replace( ':', '', $str );
	$str = str_replace( '(', '', $str );
	$str = str_replace( ')', '', $str );
	$str = str_replace( ',', '', $str );

	return $str;
	}


}
